<?php
namespace jy_nongfee\area;
defined('BY_JYA') or exit('error');
/*
	区域代理分红计算
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->areasetting['isopen']);
		if(!$is_open){
			return;
		}
		$level=intval($this->areasetting['area_level']);
		if($level==0){
			return;
		}
		$this->order=$order;
		$address=pdo_getcolumn('core_address',array('id'=>$order['addressid']),'pcd');
		$address=json_decode($address,true);
		if(!$address){
			return;
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'commission_money'=>0,
			'type'=>1,
			'createtime'=>TIMESTAMP,
		);
		if($this->areasetting['open_duodai']){
			//计算多省代
			$this->duodai_commission($address);
		}
		//计算省级代理分红
		$this->province_commission($address);
		if($this->areasetting['area_level']>1){
			//计算市级代理分红
			$this->city_commission($address);
		}
		if($this->areasetting['area_level']>2){
			//计算县级代理分红
			$this->district_commission($address);
		}
		if($this->areasetting['area_level']>3){
			//计算镇级代理分红
			$this->street_commission($address);
		}
		//生成分销订单
		if($this->commission_order['isarea']){
			$indata=$this->commission_order;
			unset($indata['isarea']);
			$this->insert('fee_order',$indata);
		}
		return true;
	}
	public function duodai_commission($address){
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents=$this->getall('area_agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'is_duodai'=>1,'duodai like'=>'%'.trim($address['province']).'%'));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('area_member',array('mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,6);
		}
	}
	public function province_commission($address){
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents=$this->getall('area_agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'area_code'=>$area_code));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('area_member',array('mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,1);
		}
	}
	public function city_commission($address){
		if(!$address['city']){
			return;
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province'].' '.$address['city']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents=$this->getall('area_agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'area_code'=>$area_code));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('area_member',array('mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,2);
		}
	}
	public function district_commission($address){
		if(!$address['district']){
			return;
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province'].' '.$address['city'].' '.$address['district']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents=$this->getall('area_agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'area_code'=>$area_code));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('area_member',array('mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,3);
		}
	}
	public function street_commission($address){
		if(!$address['street']){
			return;
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address['province'].' '.$address['city'].' '.$address['district'].' '.$address['street']),'code');
		$this->commission_order['area_code']=$area_code;
		$agents=$this->getall('area_agent_area',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'area_code'=>$area_code));
		if(!$agents){
			return;
		}
		foreach($agents as $agent){
			$member=$this->get('area_member',array('mid'=>$agent['mid']));
			if(!$member || $member['stop']){
				continue;
			}
			$this->create_commission($agent,4);
		}
	}
	/*
		给指定区域代理计算分红
	*/
	function create_commission($agent,$commission_level){
			if(!$agent['level_id']){
				//获取默认等级佣金比例
				$level_commissions=$this->plugin_setting('default_level');
				$level_id=0;
				if($commission_level==6){
					$commission_per=$level_commissions['duodai'];
				}else{
					$commission_per=$level_commissions['commission'.$commission_level];
				}
			}else{
				$level=$this->get('area_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_commissions=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				if($commission_level==6){
					$commission_per=$level_commissions['duodai'];
				}else{
					$commission_per=$level_commissions['commission'.$commission_level];
				}
			}
			$money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			
			//先检查是否存在商品独立佣金规则，如果存在则对订单相应商品部分金额单独计算佣金
			foreach($this->order['goods'] as $goods){
				$ignore=$this->get('goods',array('goods_id'=>$goods['goodsid']));
				if($ignore['ignore']  || !$ignore['id']){
					if($ignore['ignore']){
						$order_price=$order_price-$goods['total_price'];
					}
					$canorder_price=1;
					continue;
				}
				if($ignore['level_id'] && $ignore['level_id']!=$level_id){
					$canorder_price=1;
					continue;
				}
				if($canorder_price){
					$order_price=$order_price-$goods['total_price'];
				}
				$goods_commissions=$ignore['commissions']?json_decode($ignore['commissions'],true):array();
				if($commission_level==6){
					$goods_commission_per=$goods_commissions['duodai'];
				}else{
					$goods_commission_per=$goods_commissions['commission'.$commission_level];
				}
				if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($goods['total_price']*substr($goods_commission_per,0,-1))/100;
					$money+=$goods_commission_money;
				}else{
					$goods_commission_money=intval($goods_commission_per*$goods['total']*100)/100;
					$money+=$goods_commission_money;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			}
			if($order_price>0 && $canorder_price){
				$order_commission_money=intval($order_price*$commission_per)/100;
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				$commission_level=$commission_level!=6?$commission_level:5;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'buymid'=>$this->commission_order['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'type'=>$commission_level+2,
					'ordersn'=>$this->order['ordersn'],
					'jifen'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
				);
				$this->insert('commission',$new_commission);
				//$this->model('area/notice')->notice_fenxiao_feeadd($new_commission['mid'],$new_commission['money']);
			}
			//判断是否存在区域代理
			$this->commission_order['isarea']=true;
			//此处判断分销商是否满足升级条件，如果满足则升级分销商等级
			$agent_update=array(
				'order_price +='=>$this->commission_order['order_price'],
				'order_num +='=>1,
				'commission_money +='=>$money,
			);
			$this->update('area_agent_area',$agent_update,array('id'=>$agent['id']));
			$this->model('area_member')->update_agent_level($agent['id']);

	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		//$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$order['ordersn']));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		exi('统一平台定时释放！','error');
	}
}